<?php
/**
 * Created by PhpStorm.
 * User: zhangtao
 * Date: 16-5-23
 * Time: 下午4:14
 */

namespace Home\Controller;


use Think\Controller;

class SearchController extends Controller
{
    public function index()
    {
        $search = D('Store');
        $key = I('post.key');
        $condition['shop_name|dish_name'] = array('LIKE', '%' . $key . '%');
        $searchData = $search->distinct(true)->field(array('store.id','shop_name','discount','store.score','store.sell_num','ave_sendtime'))
            ->table('__DISH__ dish,__STORE__ store')->where($condition)->where('store.id=dish.store_id')->select();
        unset($condition);
        $condition['dish_name'] = array('LIKE', '%' . $key . '%');
        foreach ($searchData as $k => $v) {
            $num = $search->table('__DISH__')->field('count(*) as num')->where($condition)->where(array('store_id'=>$v['id']))->find();
            $dish = $search->table('__DISH__')->field('dish_name')->where($condition)->where(array('store_id'=>$v['id']))->limit(3)->select();
            $searchData[$k]['num']=$num['num'];
            $searchData[$k]['dish']=$dish;
        }
        $this->ajaxReturn($searchData);
       //echo json_encode($searchData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    }
}